package com.hqyj.springBoot.modules.common.service.impl;

import com.hqyj.springBoot.modules.common.dao.ExceptionLogDao;
import com.hqyj.springBoot.modules.common.entity.ExceptionLog;
import com.hqyj.springBoot.modules.common.entity.ResultEntity;
import com.hqyj.springBoot.modules.common.service.ExceptionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.net.dns.ResolverConfiguration;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {

    @Autowired
    private ExceptionLogDao exceptionLogDao;

    @Override
    @Transactional
    public ResultEntity<ExceptionLog> insertExceptionLogDao(ExceptionLog exceptionLog) {
        List<ExceptionLog> exceptionLogs = Optional.ofNullable(exceptionLogDao
                .getExceptionLogsBy(exceptionLog.getPath(),exceptionLog.getExceptionType()))
                .orElse(Collections.emptyList());//jdk8新特性先查询有没有这种类型的异常，如果为空就给一个空的收集器
        if(exceptionLogs.isEmpty()){
            exceptionLog.setCreateDate(LocalDateTime.now());//获取系统时间
            exceptionLogDao.insertExceptionLogDao(exceptionLog);
            return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"添加成功！",exceptionLog);
        }
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"异常已存在！",exceptionLog);
    }

    @Override
    public List<ExceptionLog> getExceptionLogsBy(String path, String exceptionType) {
        return exceptionLogDao.getExceptionLogsBy(path, exceptionType);
    }
}
